package com.pcbly.web.action.mail;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.ajaxanywhere.AAUtils;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.mail.UserMailBean;
import com.pcbly.web.bean.mail.UserReceiveMainBean;
import com.pcbly.web.business.mail.UserMailService;
import com.pcbly.web.business.mail.UserReceiveMailService;
import com.pcbly.web.business.mail.UserReceiveMainService;
import com.pcbly.web.business.mail.UserSendMailService;
import com.pcbly.web.business.mail.UserSendMainService;
import com.pcbly.web.components.email.MailReceive;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.utils.LightsoftStringUtils;
import com.pcbly.web.components.utils.random.GUID;

public class UserMailAction extends BaseAction<UserMailBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1868193201678053578L;

	protected UserMailService userMailService;

	protected UserReceiveMainService userReceiveMainService;

	protected UserReceiveMailService userReceiveMailService;

	protected UserSendMainService userSendMainService;

	protected UserSendMailService userSendMailService;

	protected UserMailBean userMail;

	protected String userIdUserMail;

	private Page<UserReceiveMainBean> userReceiveMainBeanPages;

	private UserReceiveMainBean userReceiveMainBean;

	protected int boxFlag;
	
	

	private String orderBy;

	private boolean asc;

	protected List<UserMailBean> userMailBeans = new ArrayList<UserMailBean>();

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isAsc() {
		return asc;
	}

	public boolean getAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public UserReceiveMainBean getUserReceiveMainBean() {
		return userReceiveMainBean;
	}

	public void setUserReceiveMainBean(UserReceiveMainBean userReceiveMainBean) {
		this.userReceiveMainBean = userReceiveMainBean;
	}

	public Page<UserReceiveMainBean> getUserReceiveMainBeanPages() {
		return userReceiveMainBeanPages;
	}

	public void setUserReceiveMainBeanPages(
			Page<UserReceiveMainBean> userReceiveMainBeanPages) {
		this.userReceiveMainBeanPages = userReceiveMainBeanPages;
	}

	public String getUserIdUserMail() {
		return userIdUserMail;
	}

	public void setUserIdUserMail(String userIdUserMail) {
		this.userIdUserMail = userIdUserMail;
	}

	public int getBoxFlag() {
		return boxFlag;
	}

	public void setBoxFlag(int boxFlag) {
		this.boxFlag = boxFlag;
	}

	public List<UserMailBean> getUserMailBeans() {
		return userMailBeans;
	}

	public void setUserMailBeans(List<UserMailBean> userMailBeans) {
		this.userMailBeans = userMailBeans;
	}

	public UserMailBean getUserMail() {
		return userMail;
	}

	public void setUserMail(UserMailBean userMail) {
		this.userMail = userMail;
	}

	public String cancel() {
		return CANCEL;
	}

	public void setUserMailService(UserMailService userMailService) {
		this.userMailService = userMailService;
	}

	public String view() throws Exception {
		userReceiveMainBean = userReceiveMainService
				.getUserReceiveMainBeanById(getId());
		return super.view();
	}

	public String index() throws Exception {
		userMailBeans = userMailService.getUserMailBeans();
		userIdUserMail = getUserId();
		userReceiveMainBeanPages = new Page<UserReceiveMainBean>();
		
		if (LightsoftStringUtils.nullOrBlank(getId())) {
			UserMailBean bean = userMailService.getMainUserMailBeanByFlag("1");
			if (bean != null) {
				setId(bean.getId());
			}
		}
		if(LightsoftStringUtils.nullOrBlank(orderBy)){
			orderBy="RECEIVEDATE";
			asc=true;
		}
		if (log.isDebugEnabled()) {
			System.out.println("page :::::::::: " + getPage());
			System.out.println("pageSize :::::::::: " + getPageSize());
			System.out.println("orderBy :::::::::: " + orderBy);
			System.out.println("asc :::::::::: " + asc);
			log.debug("page :::::::::: " + getPage());
			log.debug("pageSize :::::::::: " + getPageSize());
			log.debug("orderBy :::::::::: " + orderBy);
			log.debug("asc :::::::::: " + asc);
		}
		if (userReceiveMainBean == null) {
			userReceiveMainBean = new UserReceiveMainBean();
		}
		userReceiveMainBean.setFlag(boxFlag);
		userReceiveMainBean.setUserMailId(getId());
		userReceiveMainBeanPages.setPageNo(getPage());
		userReceiveMainBeanPages.setPageSize(getPageSize());
		userReceiveMainBeanPages = userReceiveMainService
				.findUserReceiveMainBeanByCond(userReceiveMainBean,
						userReceiveMainBeanPages,orderBy, asc);
		return SUCCESS;
	}

	public String child() throws Exception {
		userMailBeans = userMailService.getUserMailBeans();
		userIdUserMail = getUserId();
		userReceiveMainBeanPages = new Page<UserReceiveMainBean>();
		if (log.isDebugEnabled()) {
			System.out.println("page :::::::::: " + getPage());
			System.out.println("pageSize :::::::::: " + getPageSize());
			log.debug("page :::::::::: " + getPage());
			log.debug("pageSize :::::::::: " + getPageSize());
		}

		if (userReceiveMainBean == null) {
			userReceiveMainBean = new UserReceiveMainBean();
		}
		userReceiveMainBean.setFlag(boxFlag);
		userReceiveMainBean.setUserMailId(getId());
		userReceiveMainBeanPages.setPageNo(getPage());
		userReceiveMainBeanPages.setPageSize(getPageSize());
		userReceiveMainBeanPages = userReceiveMainService
				.findUserReceiveMainBeanByCond(userReceiveMainBean,
						userReceiveMainBeanPages, "", asc);
		if (AAUtils.isAjaxRequest(getRequest())) {
			AAUtils.addZonesToRefresh(getRequest(), "emailMainAjaxAnyWhere");
		}
		return SUCCESS;
	}

	public void sendMail(String id) throws Exception {
		userMail = userMailService.get(id);
		Properties props = System.getProperties();
		// props.put("mail.smtp.host", userMail.getHostName());
		// Get a Session object
		Session session = Session.getInstance(props, null);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(userMail.getUserName()));

	}

	public String save() throws Exception {
		if (userMail.getId() == null || "".equals(userMail.getId())) {
			String id = (new GUID()).toString();
			userMail.setId(id);
			userMailService.addUserMailBean(userMail);
			super.saveMessage(getText("entity.saved"));
		} else {
			userMailService.updateUserMailBean(userMail);
			super.saveMessage(getText("entity.updated"));
		}
		return super.save();
	}

	public String dwrReceiveMail(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login receiveMail.........");
			log.debug(id);
		}
		try {
			userMail = userMailService.getUserMailBeanById(id);

			List<UserReceiveMainBean> list = new ArrayList<UserReceiveMainBean>();
			MailReceive receive = new MailReceive();
			receive.setUserId(super.getDwrUserId());
			receive.setRealPath(getDwrSession().getServletContext()
					.getRealPath("/upload"));
			receive.setSaveAttachPath("/upload");
			receive.setBean(userMail);
			list = receive.receiveMail();
			if (list != null && !list.isEmpty()) {
				userReceiveMainService.saveList(list, userMail.getUserName());
			}
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}

	}

	public UserMailBean dwrView(String id) throws Exception {
		if (id == null || "".equals(id.trim())) {
			return null;
		}
		return userMailService.getUserMailBeanById(id);
	}

	public String dwrSave(UserMailBean userMail) throws Exception {
		try {
			if (userMail.getId() == null || "".equals(userMail.getId())) {
				String id = (new GUID()).toString();
				userMail.setId(id);
				userMailService.addUserMailBean(userMail);
			} else {
				userMailService.updateUserMailBean(userMail);
			}
			return super.dwrBaseSave(userMail.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
	}

	public String dwrDelete(String id) throws Exception {
		userMailService.deleteUserMailBeanById(id);
		return super.dwrBaseDelete(id);
	}

	public String saveUser() throws Exception { 
		return SUCCESS;
	}

	public UserMailAction() {
		// TODO Auto-generated constructor stub
	}

	public void setUserReceiveMainService(
			UserReceiveMainService userReceiveMainService) {
		this.userReceiveMainService = userReceiveMainService;
	}

	public void setUserReceiveMailService(
			UserReceiveMailService userReceiveMailService) {
		this.userReceiveMailService = userReceiveMailService;
	}

	public void setUserSendMainService(UserSendMainService userSendMainService) {
		this.userSendMainService = userSendMainService;
	}

	public void setUserSendMailService(UserSendMailService userSendMailService) {
		this.userSendMailService = userSendMailService;
	}

}
